//
//  ZZFToaster.m
//  zhangzhongfo
//
//  Created by 瞿衡 on 17/1/16.
//  Copyright © 2017年 瞿衡. All rights reserved.
//

#import "BZBaseToaster.h"
#import <BZPreHeaderComponent/BZPreHeaderComponent.h>
#import <Masonry/Masonry.h>

@interface BZBaseToaster()

@property (nonatomic, assign) CGRect  toasterFrame;

@property (nonatomic, weak  ) UILabel *label;

@end

@implementation BZBaseToaster




+ (void)addToastWithLeftStr:(NSString *)LeftStr
                   rightStr:(NSString *)rightStr
                   duration:(CGFloat)duration
                     inView:(UIView *)view{

    if (!duration) {
        duration = 0.6;
    }
    UIView *containerV = [[UIView alloc]init];
    containerV.backgroundColor = [UIColor colorWithRed:1.00 green:0.42 blue:0.12 alpha:1.00];
    [view addSubview:containerV];

    UILabel *leftL = [[UILabel alloc]init];
    leftL.font = [UIFont systemFontOfSize:12];
    leftL.textAlignment = NSTextAlignmentLeft;
    leftL.textColor = [UIColor whiteColor];
    [containerV addSubview:leftL];
    
    UILabel *rightL = [[UILabel alloc]init];
    rightL.font = [UIFont systemFontOfSize:12];
    rightL.textAlignment = NSTextAlignmentRight;
    rightL.textColor = [UIColor whiteColor];
    [containerV addSubview:rightL];
    
    CGFloat margin = 10;
    CGFloat leftW,rightW;
    if ([LeftStr length] && [rightStr length]) {
        leftL.text  = LeftStr;
        rightL.text = rightStr;
        leftW  = SCREEN_WIDTH-margin*2/2;
        rightW = leftW;
    }else{
        leftL.text  = LeftStr?LeftStr:rightStr;
        leftL.textAlignment = NSTextAlignmentCenter;
        leftW  = SCREEN_WIDTH;
        rightW = 0;
    }
    [containerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(view);
        make.top.equalTo(view).with.offset(64);
        make.height.mas_offset(0);
    }];
    [leftL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(containerV).with.offset(10);
        make.top.and.equalTo(containerV).with.offset(0);
        make.bottom.equalTo(containerV).with.offset(0);
        make.width.mas_offset(leftW);
    }];
    [rightL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(containerV).with.offset(-10);
        make.top.and.equalTo(containerV).with.offset(0);
        make.bottom.equalTo(containerV).with.offset(0);
        make.width.mas_offset(rightW);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [containerV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(30);
        }];
        
        [view setNeedsUpdateConstraints];
        [view updateConstraintsIfNeeded];
        containerV.alpha = 0.0;
        [UIView  animateWithDuration:duration
                          animations:^{
                              [view layoutIfNeeded];
                              containerV.alpha = 1.0;
                          } completion:^(BOOL finished) {
                              
                                  [containerV mas_updateConstraints:^(MASConstraintMaker *make) {
                                      make.height.mas_offset(0);
                                  }];
                                  
                                  [view setNeedsUpdateConstraints];
                                  [view updateConstraintsIfNeeded];
                                  
                                  [UIView animateWithDuration:duration animations:^{
                                      [view layoutIfNeeded];
                                      containerV.alpha = 0.0;
                                      
                                  } completion:^(BOOL finished){
                                      [containerV removeFromSuperview];
                                  }];
                          }];
    });
}

@end
